﻿#include "filtablemodel.h"

FilTableModel::FilTableModel(QObject *parent)
    : QAbstractTableModel{parent}
{}

int FilTableModel::rowCount(const QModelIndex &parent) const
{
    return _rows.count();
}

int FilTableModel::columnCount(const QModelIndex &parent) const
{
    return this->_columnSource.size();
}

QVariant FilTableModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case FilTableModel::RowModel:
        return QVariant::fromValue(_rows.at(index.row()));
    case FilTableModel::ColumnModel:
        return QVariant::fromValue(_columnSource.at(index.column()));
    default:
        break;
    }
    return {};
}

QHash<int, QByteArray> FilTableModel::roleNames() const
{
    return {
        {FilTableModel::RowModel,    "rowModel"   },
        {FilTableModel::ColumnModel, "columnModel"}
    };
}

void FilTableModel::clear()
{
    beginResetModel();
    this->_rows.clear();
    endResetModel();
}

QVariant FilTableModel::getRow(int rowIndex)
{
    return _rows.at(rowIndex);
}

void FilTableModel::setRow(int rowIndex, QVariant row)
{
    _rows.replace(rowIndex, row.toMap());
    Q_EMIT dataChanged(index(rowIndex, 0), index(rowIndex, columnCount() - 1));
}

void FilTableModel::insertRow(int rowIndex, QVariant row)
{
    beginInsertRows(QModelIndex(), rowIndex, rowIndex);
    _rows.insert(rowIndex, row.toMap());
    endInsertRows();
}

void FilTableModel::removeRow(int rowIndex, int rows)
{
    beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
    _rows = _rows.mid(0, rowIndex) + _rows.mid(rowIndex + rows);
    endRemoveRows();
}

void FilTableModel::appendRow(QVariant row)
{
    insertRow(rowCount(), row);
}
